// pages/index/component/foundRecommend/foundRecommend.js
import {
  articleList,articleCategoryList
} from '../../../../api/article'

import util from '../../../../utils/util'
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },
  pageLifetimes:{
    show(){
      this.loadCategory()
    }
  },
  lifetimes:{
    attached(){
     
      
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    articleCategoryId:0,
    articleCategorys:[],
    pageNum:1,
    list: [],
    index: 0,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    loadCategory(){
      articleCategoryList({
        articleType:2,
        pageSize:4,
      }).then(res=>{
          this.data.articleCategorys=res.data
          // if(res.data.length>0){
          //   this.data.articleCategoryId=res.data[0].id
          // }
          this.loadNote()
          this.setData({
            articleCategorys:[{articleType:2,id:0,name:"推荐"},...res.data],
            articleCategoryId:this.data.index
          })
      })
    },
    toTitle(e) {
      this.setData({
        articleCategoryId:e.currentTarget.dataset.id,
        index: e.currentTarget.dataset.index
      })
      this.loadNote()
    },
    loadNote() {
      if(this.data.pageNum==1){
        this.data.list=[]
      }
      articleList({
        articleType: 2,
        pageNum:this.data.pageNum,
        articleCategoryId:this.data.articleCategoryId||null,
      }).then(res => {
        console.warn(res)
        res.data.data.forEach(item => {
          if(util.isNull(item.hits)){
            item.hits=0
          }
          
          if(!util.isNull( item.articleMeta.images[0])){
            item.articleMeta.images[0]=util.thumbnail_cover( item.articleMeta.images[0],350)
          }
          // item.articleMeta.images = item.articleMeta.images.map(e=>util.thumbnail_cover(e,350))
          item.createDate = util.datetime(item.createDate)
          this.data.list.push(item)
        })
        this.setData({
          list: this.data.list
        })
      })
    },
    foundClick(){
      wx.navigateTo({
        url: '/pages/hotspot/details/details',
      })
    }
  }
})